package com.sskj.mine;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.hey.lib.HeySpinner;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.mine.asset.data.CoinTypeBean;
import com.sskj.mine.bean.BuyBalanceBean;
import com.sskj.mine.bean.PayWayBean;
import com.sskj.mine.bean.PayWayListBean;
import com.sskj.mine.bean.PlatFormBean;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.MINE_BUY)
public class BuyActivity extends BaseActivity<BuyPresenter> {
    @BindView(R2.id.right_iv)
    ImageView right_iv;
    @BindView(R2.id.ll_xxrj)
    LinearLayout ll_xxrj;
    @BindView(R2.id.ll_commit)
    LinearLayout ll_commit;
    @BindView(R2.id.tv_xxrj)
    TextView tv_xxrj;
    @BindView(R2.id.btConfirm)
    Button btConfirm;
    @BindView(R2.id.btCommit)
    Button btCommit;
    @BindView(R2.id.tvBalance)
    TextView tvBalance;
    @BindView(R2.id.tradeTypeSpinner)
    HeySpinner tradeTypeSpinner;
    @BindView(R2.id.tvType)
    TextView tvType;
    @BindView(R2.id.tv_bank_name)
    TextView tv_bank_name;
    @BindView(R2.id.tv_bank_type)
    TextView tv_bank_type;
    @BindView(R2.id.tv_bank_code)
    TextView tv_bank_code;
    @BindView(R2.id.etInputMoney)
    EditText etInputMoney;
    @BindView(R2.id.tv_bottom_name)
    TextView tv_bottom_name;
    @BindView(R2.id.tv_bottom_bank)
    TextView tv_bottom_bank;
    @BindView(R2.id.tv_bottom_bank_code)
    TextView tv_bottom_bank_code;
    @BindView(R2.id.tv_bottom_sub_branch)
    TextView tv_bottom_sub_branch;
    @BindView(R2.id.tv_bottom_sub_code)
    TextView tv_bottom_sub_code;
    @BindView(R2.id.tv_xsrj)
    TextView tv_xsrj;

    @BindView(R2.id.iv_photo)
    ImageView iv_photo;
    @BindView(R2.id.iv_img)
    ImageView iv_img;
    @BindView(R2.id.ll_photo)
    LinearLayout ll_photo;
    private List<CoinTypeBean> coinTypeBeans;
    private String type;
    private BuyBalanceBean data;
    private String pid;
    private String bankid;
    private String banktp;
    private String imgPath;
    private final int SELECT_HAND = 1000;
    private boolean isOffline = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_buy;
    }

    @Override
    public BuyPresenter getPresenter() {
        return new BuyPresenter();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, BuyActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.mine_detailsActivity1_j));
        isOffline = getIntent().getBooleanExtra("isOffline", false);
        if (isOffline) {
            ll_xxrj.setVisibility(View.GONE);
            ll_commit.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initData() {
        mPresenter.getAllCoin(false);
        mPresenter.requsetPayWayList();
        mPresenter.requsetPlatformInfo();
    }

    @Override
    protected void initEvent() {
        ClickUtil.click(right_iv, () -> {
            Intent intent = new Intent(this, BuyShellRecordActivity.class);
            intent.putExtra("type", "1");
            startActivity(intent);
        });

        ClickUtil.click(tv_xxrj, () -> {

            Intent intent = new Intent(this, BuyActivity.class);
            intent.putExtra("isOffline", true);
            startActivity(intent);

        });
        ClickUtil.click(tv_xsrj, () -> {
            BuyActivity.start(this);

        });

        ClickUtil.click(btConfirm, () -> {
            if (TextUtils.isEmpty(type)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_shellActivity2_j));
                return;
            }
            if (TextUtils.isEmpty(etInputMoney.getText().toString()) || etInputMoney.getText().toString().trim().equals("0")) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_buyActivity3_j));
                return;
            }
            mPresenter.Buy(etInputMoney.getText().toString().trim(), "", pid, bankid, banktp, "1");
        });
        ClickUtil.click(iv_photo, () -> {
            selectImage(SELECT_HAND);
        });
        ClickUtil.click(btCommit, () -> {
            if (TextUtils.isEmpty(type)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_shellActivity2_j));
                return;
            }
            if (TextUtils.isEmpty(etInputMoney.getText().toString()) || etInputMoney.getText().toString().trim().equals("0")) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_buyActivity3_j));
                return;
            }
            if (TextUtils.isEmpty(imgPath)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_buyActivity4_j));
                return;
            }
            mPresenter.Buy(etInputMoney.getText().toString().trim(), imgPath, pid, bankid, banktp, "2");
        });
    }

    public void updateUi(BuyBalanceBean data) {
        this.data = data;
        tvBalance.setText(App.INSTANCE.getString(R.string.mine_shellActivity6_j) + "    " + Double.parseDouble(data.right_code) + type);
    }

    public void updatePayUi(List<PayWayListBean> data) {
        for (PayWayListBean c : data) {
            if (c.type.equals("bankcard")) {
                tv_bank_code.setText(c.number);
                tv_bank_name.setText(c.name);
                tv_bank_type.setText(c.tip);

                bankid = c.number;
                banktp = c.tip;
            }
        }
    }

    public void updatePlatformInfo(PlatFormBean data) {

        tv_bottom_bank.setText(data.getBankname());
        tv_bottom_name.setText(data.getBankna());
        tv_bottom_bank_code.setText(data.getBankid());
        tv_bottom_sub_branch.setText(data.getBanknames());
        tv_bottom_sub_code.setText(getString(R.string.mine_dai_code) + data.getBank_nick());

    }

    public void updateBuyUi() {
        etInputMoney.setText("");
        Intent intent = new Intent(this, BuyShellRecordActivity.class);
        intent.putExtra("type", "1");
        startActivity(intent);
    }

    public void updateCoinType(List<CoinTypeBean> data) {
        this.coinTypeBeans = data;
        ArrayList<String> items = new ArrayList<>();
        for (CoinTypeBean c : data) {
            items.add(c.getPname());
        }
        tradeTypeSpinner.setItemGravity(Gravity.CENTER);
        tradeTypeSpinner.attachData(items);
        type = coinTypeBeans.get(0).getPname();
        pid = coinTypeBeans.get(0).getPid();
        mPresenter.getBalance(coinTypeBeans.get(0).getMark());
        tvType.setText(type);
        tradeTypeSpinner.setOnSelectListener(new HeySpinner.OnSelectListener() {
            @Override
            public void onSelect(int position) {
                type = coinTypeBeans.get(position).getPname();
                pid = coinTypeBeans.get(position).getPid();
                mPresenter.getBalance(coinTypeBeans.get(position).getMark());
                tvType.setText(type);
            }
        });
    }

    @SuppressLint("CheckResult")
    private void selectImage(int requestCode) {
        new RxPermissions(this)
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                .subscribe(granted -> {
                    if (granted) {
                        PictureSelector.create(this)
                                .openGallery(PictureMimeType.ofImage())
                                .previewImage(true)
                                .isCamera(true)
                                .imageFormat(PictureMimeType.JPEG)
                                .maxSelectNum(1)
                                .compress(true)
                                .forResult(requestCode);
                    }
                });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == RESULT_OK) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            if (selectList != null && selectList.size() > 0) {
                LocalMedia media = selectList.get(0);
                String url;
                if (media.isCompressed()) {
                    imgPath = media.getCompressPath();
                } else {
                    imgPath = media.getPath();
                }
                switch (requestCode) {
                    case SELECT_HAND:
                        Glide.with(this).load(imgPath).into(iv_img);
                        ll_photo.setVisibility(View.GONE);
                        iv_img.setVisibility(View.VISIBLE);
                        break;
                }
            }
        }

    }
}
